package cn.pconline.autoclub.util;

import com.alibaba.fastjson.JSONArray;
import com.danga.MemCached.MemCachedClient;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @email chensy0203@163.com
 * @author chensy
 */
public class McUtils {

    /**
     * key：前缀-品牌ID-车友会ID-时间yyyyMMdd
     */
    public final static String listTopClubWeeklyByBrandKey = "listTopClubWeeklyByBrand-%s-%s-%s";
    public final static String list5ClubWeeklyByBrandKey = "list5ClubWeeklyByBrand-%s-%s-%s";
    /**
     * key：前缀-省ID-车友会ID-时间yyyyMMdd
     */
    public final static String listTopClubWeeklyByAreaKey = "listTopClubWeeklyByArea-%s-%s-%s";
    public final static String list5ClubWeeklyByAreaKey = "list5ClubWeeklyByArea-%s-%s-%s";
    /**
     * key：前缀-车友会ID-时间yyyyMMdd
     */
    public final static String listTopClubWeeklyByNationwideKey = "listTopClubWeeklyByNationwide-%s-%s";
    public final static String list5ClubWeeklyByNationwideKey = "list5ClubWeeklyByNationwide-%s-%s";
    /**
     * key:前缀-省ID-城市ID-时间yyyyMMdd
     */
    public final static String getRecommendClubsByPidAndCidKey = "getRecommendClubsByPidAndCid-%s-%s-%s";
    private MemCachedClient mcc;
    private String key;

    public McUtils(MemCachedClient mcc, String keyFmt, Object... args) {
        this.mcc = mcc;
        this.key = String.format(keyFmt, args);
    }

    public JSONArray get() {
        String jsonStr = (String) mcc.get(key);
        if (StringUtils.isNotEmpty(jsonStr)) {
            return JSONArray.parseArray(jsonStr);
        }
        return null;
    }

    public boolean set(JSONArray arr) {
        if(arr.isEmpty()){
            return false;
        }
        return mcc.set(key, arr.toJSONString());
    }
}
